package com.pray.praymall.search.controller;

import com.pray.common.exception.BizCodeEnum;
import com.pray.common.to.es.SkuEsModel;
import com.pray.common.utils.R;
import com.pray.praymall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * ElasticSearch保存
 *
 * @author 魂悸魄
 * @date 2020/9/12 17:54
 */

@RequestMapping("search/save")
@Slf4j
@RestController
public class ElasticSaveController {

    @Autowired
    private ProductSaveService productSaveService;


    /**
     * 保存商品上架信息
     *
     * @param esModels 商品上架需要保存的数据集合
     * @return 标准返回体
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> esModels) {
        boolean b = false;
        try {
            b = productSaveService.productStatusUp(esModels);
        } catch (IOException e) {
            log.error("商品上架错误：" + e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (b) {
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        return R.ok();
    }
}
